package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.BankcardMapper;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.UserQuery;
import cn.wolfcode.trip.base.service.IUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private BankcardMapper bankcardMapper;

    /**
     * 注册
     */
    @Override
    public void register(User user) {
        User temp = userMapper.selectByEmailAndPassword(user.getEmail(), null);
        if (temp != null) {
            //已经注册过了
            throw new RuntimeException("邮箱已经注册了...");
        }
        user.setHeadImgUrl("/img/user/head.jpg");
        user.setCoverImgUrl("/img/user/head.jpg");
        userMapper.insert(user);
    }

    @Override
    public User login(String email, String password) {
        User temp = userMapper.selectByEmailAndPassword(email, password);
        if (temp == null) {
            //登录失败
            throw new RuntimeException("邮箱与密码不匹配...");
        }
        return temp;
    }

    /**
     * 分页查询
     *
     * @param qo
     * @return
     */
    public PageInfo<?> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<?> list = userMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public void update(User user) {
        int count = userMapper.updateByPrimaryKey(user);
        if (count == 0) {
            //受影响行数为0，更新失败，抛出异常
            throw new RuntimeException("数据出错，更新失败！！！");
        }
    }

    @Override
    public User selectByUserId(Long authorId) {
        return userMapper.selectByPrimaryKey(authorId);
    }

    @Override
    public int getFansByCount(Long id) {
        return userMapper.selectFansByCount(id);
    }

    @Override
    public int getFollowByCount(Long id) {
        return userMapper.selectFollowByCount(id);
    }

    @Override
    public PageInfo<?>  selectFollowAndFansById(UserQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        if (qo.getFollowId() != null) {
            return new PageInfo<>(userMapper.selectFollowById(qo));
        } else if (qo.getFansId() != null) {
            return new PageInfo<>(userMapper.selectFansById(qo));
        }
        return null;
    }

    @Override
    public User getById(Long id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public int checkFollowAndFans(UserQuery qo) {
        return userMapper.checkFollowAndFans(qo);
    }

    @Override
    public void deleteUserByFans(UserQuery qo) {
        userMapper.deleteUserByFans(qo);
    }

    @Override
    public void saveUserByFans(UserQuery qo) {
        //插入之前，删除他们的关系
        userMapper.deleteUserByFans(qo);
        userMapper.insertUserByFans(qo);
    }

    @Override
    public void updateBalance(User user) {
        userMapper.updateBalance(user);
    }

    /**
     * 修改钱包的金额
     *
     * @param user
     */
    @Override
    public void updateMoney(User user) {
        //查询余额
        User u = userMapper.selectByPrimaryKey(user.getId());
        if (user.getBalance() > u.getBalance()) {
            throw new RuntimeException("提现的余额不能大于余额!");
        }
        userMapper.updateMoney(user);
    }
}
